from typing import Tuple


class InputHandler:
    """输入处理工具类"""
    
    @staticmethod
    def get_credentials() -> Tuple[str, str]:
        """获取用户登录凭据"""
        # print("\033[1;34m请输入登录信息\033[0m")
        
        # 使用getpass隐藏密码输入
        account = input("\033[1;32m请输入账号：\033[0m").strip()
        if not account:
            raise ValueError("账号不能为空")
        
        password = input("\033[1;32m请输入密码：\033[0m").strip()
        if not password:
            raise ValueError("密码不能为空")
        
        return account, password
    
    @staticmethod
    def select_course(courses: list) -> int:
        """选择课程"""
        if not courses:
            raise ValueError("没有找到可用的课程")
        
        print("\n\033[1;34m请选择要下载的课程：\033[0m")
        for i, course in enumerate(courses):
            print(f"  {i}: {course['productName']}")
        
        while True:
            try:
                choice = int(input("\033[1;34m请输入课程序号：\033[0m"))
                if 0 <= choice < len(courses):
                    return choice
                else:
                    print(f"\033[1;31m请输入0-{len(courses)-1}之间的数字\033[0m")
            except ValueError:
                print("\033[1;31m请输入有效的数字\033[0m")
    
    @staticmethod
    def get_custom_path(prompt: str, default_path: str) -> str:
        """获取自定义路径"""
        path = input(f"\033[1;32m{prompt} (默认: {default_path}): \033[0m").strip()
        return path if path else default_path